const express = require('express');
const bodyParser = require('body-parser');
const passport= require('passport')

const mongoose = require('mongoose');

const session = require('./routers/session.js');
const profile = require('./routers/profile.js');
// DB config
const db = require('./config/mongo').mongoURI;

const app = express();

// 连接mongoDB数据库服务  直接在app中连接数据库，可以一次连接多处使用
mongoose.connect(db)
    .then(() => console.log('MongoDB Connected'))
    .catch(err => console.log(err));
// 对mongoose进行简单设置
mongoose.set('useFindAndModify', false)    



const port = process.env.PORT || 5000;

//  配置解析post请求的工具，一定要在路由挂载之前。(中间件)
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// passport初始化
app.use(passport.initialize());
require('./config/passport')(passport);  // 单独写一个js文件用来专门书写加载的token验证策略

// 将路由挂载到app服务中
app.use(session);
app.use(profile);

app.listen(port, () => {
    console.log(`Server is running on ${port}`);
})